﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web_cuiki
{
    public class KieuSPDAO
    {
        public static List<KIEU_SAN_PHAM> laydsKieuSanPham()
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<KIEU_SAN_PHAM> ds = null;
            var query = from ksp in db.KIEU_SAN_PHAMs
                        select ksp;
            ds = query.ToList<KIEU_SAN_PHAM>();
            return ds;
        }
        public static KIEU_SAN_PHAM TimKieuSanPhamTheoMa(int maKieu)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            var query = from km in db.KIEU_SAN_PHAMs where km.MaKieuSanPham == maKieu select km;
            return query.First<KIEU_SAN_PHAM>();
        }
        public static KIEU_SAN_PHAM TimKieuSanPhamTheoTen(string tenKieu)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            var query = from km in db.KIEU_SAN_PHAMs where km.TenKieuSanPham == tenKieu select km;
            return query.First<KIEU_SAN_PHAM>();
        }
        public static int ThemKieuSanPham(string tenKieu) {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<KIEU_SAN_PHAM> table = db.GetTable<KIEU_SAN_PHAM>();
            try
            {
                KIEU_SAN_PHAM km = new KIEU_SAN_PHAM();
                km.TenKieuSanPham = tenKieu;
                table.InsertOnSubmit(km);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static int SuaKieuSanPhamTheoMa(int maKieu, string tenKieu) {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<KIEU_SAN_PHAM> table = db.GetTable<KIEU_SAN_PHAM>();
            var query = from km in db.KIEU_SAN_PHAMs where km.MaKieuSanPham == maKieu select km;
            KIEU_SAN_PHAM khuyenmai = query.First<KIEU_SAN_PHAM>();
            try
            {

                khuyenmai.TenKieuSanPham = tenKieu;
                // table.InsertOnSubmit(km);

                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static int XoaTheoMa(int maKM)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            try
            {
                var query = from hd in db.KIEU_SAN_PHAMs where hd.MaKieuSanPham == maKM select hd;
                KIEU_SAN_PHAM hoadon = query.First<KIEU_SAN_PHAM>();
                System.Data.Linq.Table<KIEU_SAN_PHAM> table = db.GetTable<KIEU_SAN_PHAM>();
                table.DeleteOnSubmit(hoadon);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
    }
}